from conf.config import MYSQL_CONFIG
from db.mysql.mysql_db import MysqlClient


class DecorationTitleDAO(object):
    """ 家装标题数据访问对象 """
    def __init__(self, *args, **kwargs):
        super(DecorationTitleDAO, self).__init__(*args, **kwargs)

    @staticmethod
    def get_mysql_conn():
        """ 获取mysql连接 """
        mysql_conn = MysqlClient(MYSQL_CONFIG)
        return mysql_conn

    def get_title_info(self, title_id: int) -> dict:
        """
        获取标题信息
        :param title_id: 标题id
        :return:
        """
        mysql_conn = self.get_mysql_conn()
        title_info = dict()
        try:
            sql = ('SELECT title_id, title, title_tag '
                   'FROM decoration_title '
                   'WHERE title_id = %s;')
            args = (title_id,)
            title_info = mysql_conn.fetchone(sql, args=args)
        finally:
            if "mysql_conn" in dir():  # 判断连接是否成功创建，创建了才能执行close()
                mysql_conn.close()

        return title_info


def main():
    decoration_title_dao = DecorationTitleDAO()
    res = decoration_title_dao.get_title_info(title_id=1)
    print(f"{res}")


if __name__ == '__main__':
    main()
